package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-04-14:10
 */
@Controller
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    /**
     * 根据分类查询分组
     *
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid") Long cid) {
        if (cid <= 0) {
            //请求参数不合法
            return ResponseEntity.badRequest().build();
        }
        //查询分类下分组
        List<SpecGroup> specGroups = this.specificationService.querySpecGroupByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)) {
            //404 资源服务未找到
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }

    /**
     * 修改分组数据
     *
     * @param specGroup
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> editSpecGroup(@RequestBody SpecGroup specGroup
    ) {
        this.specificationService.editSpecGroupByGroup(specGroup);
        //204 操作执行成功
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增分组数据
     *
     * @param specGroup
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroup specGroup) {
        this.specificationService.saveSpecGroup(specGroup);
        //201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除分组数据
     *
     * @param cid
     * @param gid
     * @return
     */
    @DeleteMapping("group")
    public ResponseEntity<Void> deleteSpecGroup(@RequestParam("cid") Long cid, @RequestParam("gid") Long gid) {
        this.specificationService.deleteSpecGroup(cid, gid);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 添加规格参数
     *
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParam specParam) {
        this.specificationService.saveSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新规格参数
     *
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam) {
        this.specificationService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除规格参数
     *
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id") Long id) {
        this.specificationService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 查询规格参数
     *
     * @param cid       分类
     * @param gid       组
     * @param searching 是否搜索
     * @param generic   是否通用
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParamByCid(
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "generic", required = false) Boolean generic
    ) {
        List<SpecParam> specParams = this.specificationService.querySpecParams(cid, gid, searching, generic);
        if (CollectionUtils.isEmpty(specParams)) {
            //404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }

    /**
     * 根据分类查询组和规格参数数据
     *
     * @param cid
     * @return
     */
    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupAndSpecParamsByCid(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = this.specificationService.querySpecGroupAndSpecParamsByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)) {
            //404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }

}
